package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.CoverTheme;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class VoiceCardCoverThemeMoreAdapter extends BaseAdapter {
    private Context mContext;
    private List<CoverTheme> mItems;

    public VoiceCardCoverThemeMoreAdapter(Context context, List<CoverTheme> mItems) {
        this.mContext = context;
        this.mItems = mItems;
    }

    public int getCount() {
        return mItems.size();
    }

    public Object getItem(int position) {
        return mItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.voicecard_covertheme_more_item, null, false);
            holder.iv = (SimpleDraweeView) convertView.findViewById(R.id.covertheme_item_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, getHigh(mContext)));
        final String picurl = mItems.get(position).getOfficialCoverPicture();
        ImageOptHelper.showFrescoCoverTheme(Uri.parse(picurl), holder.iv, mContext);
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.sendBroadcast(new Intent(ACTIONs.actionUpdateCoverImg).putExtra("url", picurl));
            }
        });
        return convertView;
    }

    private class ViewHolder {
        SimpleDraweeView iv;
    }

    public int getHigh(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int pwidth = wm.getDefaultDisplay().getWidth();
        int imgwidth = pwidth - DisplayUtils.dp2px(context, 40);
        int singleimgwidth = imgwidth / 2;
        return singleimgwidth * 460 / 326;
    }

}
